package com.ambulance.project.biz.controller.pad;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ambulance.common.exception.CustomException;
import com.ambulance.framework.aspectj.lang.annotation.Log;
import com.ambulance.framework.aspectj.lang.enums.BusinessType;
import com.ambulance.framework.aspectj.lang.enums.OperatorType;
import com.ambulance.framework.web.domain.AjaxResult;
import com.ambulance.framework.web.domain.AjaxResultData;
import com.ambulance.project.biz.dto.pad.NhissAnswerDto;
import com.ambulance.project.biz.dto.pad.NhissDto;
import com.ambulance.project.biz.entity.Dispatch;
import com.ambulance.project.biz.entity.DispatchCase;
import com.ambulance.project.biz.entity.Nhiss;
import com.ambulance.project.biz.entity.NhissAnswer;
import com.ambulance.project.biz.service.DispatchCaseService;
import com.ambulance.project.biz.service.NhissAnswerService;
import com.ambulance.project.biz.service.NhissService;
import com.ambulance.project.manager.utils.DoctorAndNurseCommonUtil;
import com.ambulance.utils.ChildUtils;
import com.ambulance.utils.CommonUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;


@Api(tags = {"随车医生-NHISS答案相关接口"})
@Controller
@RequestMapping(value = "/pad/v1/nhiss/answer", method = RequestMethod.POST)
public class NhissAnswerController {

    @Autowired
    private NhissAnswerService nhissAnswerService;
    @Autowired
    private NhissService nhissService;
    @Autowired
    private DoctorAndNurseCommonUtil doctorAndNurseCommonUtil;
    @Autowired
    private DispatchCaseService dispatchCaseService;

    @RequestMapping(value = "/add")
    @ResponseBody
    @ApiOperation(value = "保存Nhiss答案", notes = "保存Nhiss答案", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "答案json串 {key(题干的id):value(答案的id)}", required = true, paramType = "form", dataType = "String"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    @Log(title = "填写nhiss答案", businessType = BusinessType.OTHER, operatorType = OperatorType.MOBILE)
    public Object add(
            @RequestParam(value = "content") String content
    ) {
        final Dispatch dispatchInfo = doctorAndNurseCommonUtil.getDispatch();
        final DispatchCase dispatchCaseServiceOne = dispatchCaseService.getOne(new QueryWrapper<DispatchCase>().eq("dispatchID", dispatchInfo.getDispatchID()));
        NhissAnswer dcids = nhissAnswerService.getOne(new QueryWrapper<NhissAnswer>().eq("dcid", dispatchCaseServiceOne.getDispatchCaseid()));
        if (!CommonUtils.checkNull(dcids)) {
            nhissAnswerService.removeById(dcids.getId());
//            throw  new ClassCastException("不可重复提交NHiss评分");
        }
        Map maps = JSON.parseObject(content, Map.class);
        int sum = 0;
        for (Object obj : maps.keySet()) {
            final Nhiss nhissServiceOne = nhissService.getOne(new QueryWrapper<Nhiss>().eq("id", Integer.parseInt(maps.get(obj).toString())));
            if (!CommonUtils.checkNull(nhissServiceOne)) {
                sum += Integer.parseInt(nhissServiceOne.getNumber());
            }
        }
        final NhissAnswer nhissAnswer = new NhissAnswer();
        nhissAnswer.setContent(content);
        nhissAnswer.setDcid(dispatchCaseServiceOne.getDispatchCaseid());
        nhissAnswer.setSum(sum);
        nhissAnswerService.save(nhissAnswer);
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("sum", sum);
        return AjaxResult.success(map);
    }

    @ApiOperation(value = "获取NHISS答案", notes = "获取NHISS答案", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dispatchId", value = "调度id", required = true, paramType = "form", dataType = "int"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    @RequestMapping(value = "/getAnswer")
    @ResponseBody
    public Object getAnswer(@RequestParam(value = "dispatchId") Integer dispatchId) {
        final DispatchCase dispatchID = dispatchCaseService.getOne(new QueryWrapper<DispatchCase>().eq("dispatchID", dispatchId));
        Integer dcid = dispatchID.getDispatchCaseid();
        final QueryWrapper<NhissAnswer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dcid", dcid);
        final NhissAnswer one = nhissAnswerService.getOne(queryWrapper);
        if (CommonUtils.checkNull(one)) {
//            throw new CustomException("暂无NHISS信息");
            final List<Nhiss> list = nhissService.list();
            return AjaxResult.success(ChildUtils.getNhissChild(list));
        }
        final JSONObject object = JSON.parseObject(one.getContent());
        final List<Nhiss> list = ChildUtils.getNhissChild(nhissService.list());
        for (Nhiss nhiss : list) {
            nhiss.setAnswer(object.getInteger(String.valueOf(nhiss.getId())));
        }
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "APP-展示NHISS答案", notes = "展示NHISS答案", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dispatchId", value = "调度id", required = true, paramType = "form", dataType = "int"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, response = AjaxResultData.class, message = "响应")
    })
    @RequestMapping(value = "/appGetAnswer")
    @ResponseBody
    public Object getAnswers(@RequestParam(value = "dispatchId") Integer dispatchId) {
        final DispatchCase dispatchID = dispatchCaseService.getOne(new QueryWrapper<DispatchCase>().eq("dispatchID", dispatchId));
        Integer dcid = dispatchID.getDispatchCaseid();
        final QueryWrapper<NhissAnswer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dcid", dcid);
        final NhissAnswer one = nhissAnswerService.getOne(queryWrapper);
        if (CommonUtils.checkNull(one)) {
            throw new CustomException("暂无gcs信息");
        }
        final JSONObject object = JSON.parseObject(one.getContent());
        final Set<String> strings = object.keySet();
        final NhissAnswerDto nhissAnswerDto = new NhissAnswerDto();
        final ArrayList<NhissDto> nhissDtos = new ArrayList<>();
        for (String key : strings) {
            final String value = object.getString(key);
            final Nhiss topicInfo = nhissService.getOne(new QueryWrapper<Nhiss>().eq("id", key));
            final Nhiss answerInfo = nhissService.getOne(new QueryWrapper<Nhiss>().eq("id", value));
            final NhissDto nhissDto = new NhissDto();
            nhissDto.setTopic(topicInfo.getValue());
            nhissDto.setAnswer(answerInfo.getValue());
            nhissDto.setScore(Integer.parseInt(answerInfo.getNumber()));
            nhissDtos.add(nhissDto);
        }
        nhissAnswerDto.setSum(one.getSum());
        nhissAnswerDto.setNhissDto(nhissDtos);
        return AjaxResult.success(nhissAnswerDto);
    }
}
